<script setup lang="ts">
import { ref } from 'vue';

import { notification } from 'ant-design-vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

import { login as userLogin } from '../api/index'

import { encrypt, ensureKeyLength } from '../utils/common';

const store = useStore();


let password = ref("")
let email = ref("")

const secretKey = ensureKeyLength('cbhhawe123-keyword-pass-secretkey')
const router = useRouter();

const login = async () => {
    try {
        // 处理密码数据
        if (password.value === "" || email.value === "") {
            var config = {
                message: "登录失败",
                description: password.value === "" ? "请输入你的密码~" : "请输入你的邮箱~",
            }

            return notification.warning(config)
        }
        const passwordHash = encrypt(password.value, secretKey);

        const requestData = {
            password: passwordHash,
            email: email.value,
        }

        const postOption = {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(requestData),
        };
        const response = await userLogin(postOption);
        var config = {
            message: "登录成功",
            description: "欢迎回来~",
        }
        if (response.status !== "200") {
            config.message = "登录失败"
            config.description = response.message
            return notification.error(config)
        }
        // 将用户数据保存到vuex的login模块状态管理中
        notification.success(config)
        // 使用 Vue Router 进行页面导航，而不是直接修改 window.location.href

        router.push("/dashboard")

        store.commit("user/setUserData", response.result.userInfo)
        // 处理登录成功的逻辑
    } catch (error: any) {
        config = {
            message: "登录失败",
            description: error.message || "未知错误",
        }
        return notification.warning(config)
    }
}
</script>

<template>
    <div class="login-container">
        <!-- 放置logo图片 -->
        <div class="logo_img">
            <img src="../assets/avatar.png" alt="logo" />
        </div>

        <form @submit.prevent="login">
            <div class="form-group">
                <label class="submit_label" for="email">邮&emsp;箱：</label>
                <input v-model="email" type="text" id="email" placeholder="请输入你的邮箱~" />
            </div>
            <div class="form-group">
                <label class="submit_label" for="password">密&emsp;码：</label>
                <input v-model="password" type="password" id="password" placeholder="请输入你的密码~" />
            </div>
            <div class="form-group">
                <button class="login_btn">登录</button>
            </div>
        </form>

        <!-- 放置用户协议，以及忘记密码，注册新用户 -->
        <div class="user-agreement">
            <!-- 用户协议选择框 -->
            <a-flex>
                <input type="checkbox" id="agreementCheckbox"> &nbsp;
                <label class="agreement_label common_style" for="agreementCheckbox">我同意该《<a
                        href="javascript:;">用户协议</a>》</label>
            </a-flex>
            <!-- 短竖线 -->
            <span class="line_style">|</span>
            <!-- 忘记密码链接 -->
            <a class="forgot_password_label common_style" href="forgot_password.html" target="_blank">忘记密码</a>
            <span class="line_style">|</span>
            <!-- 注册新用户链接 -->
            <a href="http://localhost:8080/#/register" class="registry_user_class common_style" target="_self">注册新用户</a>
        </div>
    </div>
</template>



<style scoped lang="scss">
.user-agreement {
    display: flex;
    justify-content: space-between;

    #agreementCheckbox {
        width: 20px;
    }

    .line_style {
        font-size: 14px;
        color: #878383;
        padding: 2px;
    }

    .common_style {
        font-size: 14px;
        padding: 2px;

        &:hover {
            color: #4caf50;
        }
    }
}

.login-container {
    display: flex;
    user-select: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background-color: #f2e6e6;
    /* 登录页面背景颜色过渡色，唯美 */
    background-image: linear-gradient(to right, #f2e6e6, #d8c1c1);
}

.logo_img {
    width: 100px;
    height: 100px;
    margin-bottom: 20px;
    border-radius: 50%;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        object-position: center;
        user-select: none;

        &:hover {
            transform: scale(1.05);
            transition: all 0.3s;

        }
    }
}

.login_btn {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    background-color: #4caf50;
    color: #fff;
}

.form-group {
    display: flex;
    margin-bottom: 20px;
    align-items: center;
}

.submit_label {
    width: 100px;
    height: 100%;
    user-select: none;
    display: block;
    color: #666;
}

input {
    width: 100%;
    padding: 8px;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 4px;
}

button {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    background-color: #4caf50;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background-color: #45a049;
}
</style>